package ciyuan.Item.SkillBook;

import ciyuan.Item.ItemGroups;
import ciyuan.Item.TypeInterface.SkillBookType;
import ciyuan.Player.Skill.SkillUtils;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.item.ArmorStandEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.particles.ParticleTypes;
import net.minecraft.potion.EffectInstance;
import net.minecraft.potion.Effects;
import net.minecraft.util.DamageSource;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.RayTraceContext;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.server.ServerWorld;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Adjudication extends SkillBookType {

    public Adjudication() {
        super(new Properties().tab(ItemGroups.SkillBook));
    }

    @Override
    public void keyDown(PlayerEntity playerEntity) {
        SkillUtils.setSkillColdInterval(playerEntity, this, 10 * 1000);

        SkillUtils.THREAD_POOL_EXECUTOR.execute(new Runnable() {
            @Override
            public void run() {
                List<LivingEntity> hurts = new ArrayList<>();
                BlockPos blockPos = SkillUtils.getPlayerSee(playerEntity.level, playerEntity, RayTraceContext.FluidMode.SOURCE_ONLY, 3).getBlockPos();
                if (playerEntity.level instanceof ServerWorld) {
                    for (double i=blockPos.getX()-3;i<blockPos.getX()+3;i=i+0.1) {
                        for (double j=blockPos.getZ()-3;j<blockPos.getZ()+3;j=j+0.1) {
                            if (blockPos.distSqr(i,blockPos.getY()+1,j,false)>8 && blockPos.distSqr(i,blockPos.getY()+1,j,false)<9 ) {
                                ((ServerWorld)playerEntity.level).sendParticles(ParticleTypes.LAVA, i , blockPos.getY()+1, j, 0, 0, 0.0D, 0, 0.0D);
                            }
                        }
                    }
                }

                for(LivingEntity livingentity : playerEntity.level.getEntitiesOfClass(LivingEntity.class, new AxisAlignedBB(
                        blockPos.getX()+7,blockPos.getY()+7,blockPos.getZ()+7,blockPos.getX()-7,blockPos.getY()-7,blockPos.getZ()-7
                ))) {
                    if (livingentity != playerEntity && !playerEntity.isAlliedTo(livingentity) && (!(livingentity instanceof ArmorStandEntity) || !((ArmorStandEntity)livingentity).isMarker()) && blockPos.distSqr(livingentity.getX(),livingentity.getY(),livingentity.getZ(),false) < 18.0D) {
                        if (playerEntity.level instanceof ServerWorld) {
                            livingentity.addEffect(new EffectInstance(Effects.MOVEMENT_SLOWDOWN, 100, 3));
                            livingentity.hurt(DamageSource.playerAttack(playerEntity), 10);
                            hurts.add(livingentity);
                            double d0 = -MathHelper.sin(playerEntity.yRot * ((float) Math.PI / 180F));
                            double d1 = MathHelper.cos(playerEntity.yRot * ((float) Math.PI / 180F));
                            playerEntity.level.playSound(null, playerEntity.getX(), playerEntity.getY(), playerEntity.getZ(), SoundEvents.PLAYER_ATTACK_SWEEP, playerEntity.getSoundSource(), 1.0F, 1.0F);
                        }
                    }
                }
                SkillUtils.sleep(5000);
                for (LivingEntity livingEntity:hurts) {
                    if (livingEntity.isAlive()) {
                        livingEntity.hurt(DamageSource.playerAttack(playerEntity), 10);
                        playerEntity.level.playSound(null,livingEntity.getX(), livingEntity.getY(),livingEntity.getZ(), SoundEvents.GENERIC_EXPLODE, playerEntity.getSoundSource(), 1.0F, 1.0F);
                        if (playerEntity.level instanceof ServerWorld) {
                            for (int i=0;i<5;i++) {
                                Random random = new Random(System.currentTimeMillis());
                                ((ServerWorld)playerEntity.level).sendParticles(ParticleTypes.LAVA, livingEntity.getX() ,livingEntity.getY()+2, livingEntity.getZ(), 0,random.nextDouble() , 0.0D, random.nextDouble(), 0.0D);
                            }
                        }
                    }
                }
            }
        });

        SkillUtils.saveSkillCastTime(playerEntity,this);
    }

    @Override
    public String getSkillCastName() {
        return "Adjudication";
    }

    @Override
    public ITextComponent getSkillDescription() {
        return new TranslationTextComponent("ciyuan.skill.desc.15");
    }

    @Override
    public ITextComponent getSkillName() {
        return new TranslationTextComponent("ciyuan.skill.name.15");
    }
}
